<template>
	<view class="timetable">
		<form @submit="formSubmit" class="formView">
			
			<view class="yearView">
				<view class="yearText">
					学年
				</view>
				<picker @change="bindPickerChange" :value="formData.year" :range="array">
				    <view class="uniInput">{{array[formData.year]}}</view>
				</picker>
			</view>
		
		<view class="termView">
			<label class="radio" @click="radio('1')">
				<radio value="1" class="termRadio1" :checked="formData.term=='1'" @click="radio('1')"/><text>第一学期</text>
			</label>
			<label class="radio" @click="radio('2')">
				<radio value="2" class="termRadio2" :checked="formData.term=='2'" @click="radio('2')"/><text>第二学期</text>
			</label>	
		</view>			
		   
		<view class="usernameView">
			<input placeholder="请输入学号" type="number" v-model="formData.username" maxlength=10/>
		</view>	   
		
		<view class="passwordView">
			<input type="text" value="" password="true" placeholder="请输入密码" v-model="formData.password"/>
		</view>
		
		<button type="primary" form-type="submit">确认</button>
		</form>
	</view>
</template>

<script>
	export default{
		 data() {
		        return {
					currentYear: new Date().toISOString().slice(0, 4),
					currentMounth: new Date().toISOString().slice(5, 7),
					array: ['2018-2019', '2019-2020', '2020-2021', '2021-2022'],
					formData:{
						year: 0,
						term:'1',
						username:'',
						password:''
					},
					utid:{
						// 用户id
						uid:'',
						// 课表id
						tid:''
					}
		        }
		    },
		    computed: {
		    
		    },
			onLoad() {
				this.getYear();
				this.getMounth();
				this.$request({
				  method: 'GET', // 默认GET
				  url: '/table/upload',
				  data:{
				  				year:this.formData.year,
				  				term:this.formData.term,
				  				username:this.formData.username,
				  				password:this.formData.password
				  			},
				  // header: {}, // 可不填，虽然多半不可能
				}).then(res => {
				  // 成功后执行，具体格式待对完接口完善
				  console.log("res")
				}).catch(res => {
				  // 失败后执行
				  console.log("失败后执行")
				})
			},
		    methods: {
		        bindPickerChange: function(e) {
		            this.year = e.target.value
		        },
				radio(e){
					this.formData.term=e;
				},
				formSubmit(e){
					let data = this.formData;
					console.log(JSON.stringify(data))
					// this.getTableUpload().then(val => {
					//     this.gotimetableInfo(this.utid.tid);
					// });
					this.getTableUpload();
					// this.gotimetableInfo(this.utid.tid);
					console.log("ooooo")
				},
				getYear(){
					if(this.currentYear==2021){
						this.formData.year=3
					}
				},
				getMounth(){
					if(this.currentMounth=='12'||this.currentMounth=='01'||this.currentMounth=='02'||this.currentMounth=='03'||this.currentMounth=='04'||this.currentMounth=='05'||this.currentMounth=='06'){
						this.formData.term='1'
					}
					else{
						this.formData.term='2'
					}
				},
				// getTableUpload(){
				// console.log("哈哈哈哈哈哈")
				// this.$request({
				//   method: 'GET', // 默认GET
				//   url: '/table/upload',
				//   data:{
				//   				year:this.formData.year,
				//   				term:this.formData.term,
				//   				username:this.formData.username,
				//   				password:this.formData.password
				//   			},
				//   // header: {}, // 可不填，虽然多半不可能
				// }).then(res => {
				//   // 成功后执行，具体格式待对完接口完善
				//   console.log("res")
				// }).catch(res => {
				//   // 失败后执行
				//   console.log("失败后执行")
				// })
				// },
				
				
				getTableUpload(){
			    return new Promise((resolve, reject) => {
				uni.request({
					url:"http://yapi.smart-xwork.cn/mock/82270/v1/table/upload",
					data:{
						year:this.formData.year,
						term:this.formData.term,
						username:this.formData.username,
						password:this.formData.password
					},
					method:"GET",
					
				}).then((response) => {
      setTimeout(function() {
        uni.hideLoading();
      }, 200);
      response = response || '';
      let [error, res, reason] = response;
      resolve(res.data);
	  console.log(res.data);
	  this.utid.uid=res.data.data.uid;
	  this.utid.tid=res.data.data.tid;
	  console.log(this.utid.tid)
	  this.gotimetableInfo(this.utid.tid);
    }).catch((error) => {
      error = error || '';
      let [err, res, reason] = error;
      reject(err)
    })
  });
			},
			gotimetableInfo(tid) {
				console.log(tid)
				uni.navigateTo({
					url: "timetableInfo?tid=" +tid
			
				})
			}
			
				// success: (res) => {
				// 						this.utid.uid=res.data.data.uid;
				// 						this.utid.tid=res.data.data.tid;
				// 						console.log(this.utid.tid)
									       
				// 					    }
			
			// .then((tid)=>{
			// 			console.log(tid)
			// 			uni.navigateTo({
			// 				url: "timetableInfo?tid=" +tid
			// 		})	
			// }
		}
	}
</script>

<style lang="scss">
	page{
		background-color: $u-type-info-light;
	}
	.timetable{
		    background-color: white;
			margin-top: 100rpx;
			margin-left: 100rpx;
			margin-right: 100rpx;
			padding: 50rpx;
			padding-top: 30rpx;
			padding-bottom: 30rpx;
			font-size: 35rpx;
			color: $u-content-color;
			border-radius: 10rpx;
			box-shadow: 1px 1px 1px 1px $u-type-info-disabled;
			.yearView{
				display: flex;
				justify-content: center;
				margin-top: 10rpx;
				.yearText{
					padding: 10rpx;
				}
				.uniInput{
					margin-left: 40rpx;
					background-color: $u-type-info-light;
					padding: 10rpx;
					padding-left: 70rpx;
					padding-right: 70rpx;
					border-radius: 10rpx;
					color: $u-tips-color;
				}
			}
			.termView{
				margin-top: 30rpx;
				display: flex;
				justify-content: center;
				.termRadio1{
					transform:scale(0.7);
				}
				.termRadio2{
					transform:scale(0.7);
					margin-left: 30rpx;
				}
			}
			.usernameView{
				margin-top: 30rpx;
				background-color: $u-type-info-light;
				padding: 10rpx;
				padding-left: 30rpx;
				border-radius: 10rpx;
				color: $u-tips-color;
			}
			.passwordView{
				// width: 500rpx;
				margin-top: 30rpx;
				background-color: $u-type-info-light;
				padding: 10rpx;
				padding-left: 30rpx;
				border-radius: 10rpx;
				color: $u-tips-color;
			}
			button{
				margin-top: 30rpx;
			}
	
	}
</style>
